<?php
namespace app\admin\controller;
use think\Db;
use app\admin\model\CommonModel;


/**
 * By：wl
 * Date：2018.11
 * Function:后台管理员列表
 */
class Adminuser extends Base
{
    public function index()
    {   
        //条件筛选
        $phone_s='';
        $groupid_s='';
        $username_s='';
        $where='';

        if($_GET){
            //like
            $phone_s = $_GET['phone_s']?$_GET['phone_s']:'';
            if($phone_s){
                $where['phone'] = ['like', "%{$phone_s}%"];
            }

            //like
            $username_s = $_GET['username_s']?$_GET['username_s']:'';
            if($username_s){
                $where['username'] = ['like', "%{$username_s}%"];
            }

            //等于
            $groupid_s = $_GET['groupid_s']?$_GET['groupid_s']:'';
            if($groupid_s){
                $where['groupid'] = ['=',$groupid_s];
            }
        }
        $this->assign('phone_s', $phone_s);
        $this->assign('groupid_s', $groupid_s);
        $this->assign('username_s', $username_s);

        //分页查询
        $orderBy = ['id' => 'desc'];
        $lists = Db::table('adminuser')->where('status','<>',3)->where($where)->order($orderBy)->paginate(12,false,['query' => request()->param()]);


        $page = $lists->render();
        $lists = $lists->all(); 
        foreach ($lists as $k => $v) {
            $lists[$k]['lastlogin'] = date('Y-m-d h:i:s',$lists[$k]['lastlogin']);
            $lists[$k]['groupname'] = Db::table('admingroup')->where('id',$v['groupid'])->find()['groupname'];
        }
        $this->assign('lists', $lists);
        $this->assign('page', $page);
   
        //角色查询
        $admingroup_list = Db::query('select * from admingroup where status = 1');
        // print_r($admingroup_list);
        $this->assign("admingroup_list",$admingroup_list);

    	return view();
    }
    public function add_edit()
    {
        $data = $_GET;
        // print_r($data);
        if($data['id']){
            unset($data['password']);
        }else{
            $data['password'] = md5($data['password']);
        }
        
        $res = CommonModel::write($data,'adminuser');
        if($res){
            echojson(true,1,'操作成功');
        }else{
            echojson(false,0,'操作失败');
        }
    }
    public function del(){
        $id = $_POST['id'];

        $res = Db::table('adminuser')
                        ->where('id', $id)
                        ->update([
                            'status' => 3,
                        ]);
        if($res){
            echojson(true,1,'操作成功');
        }else{
            echojson(false,0,'操作失败');
        }
    }
   
}

